package com.itheima.service.db.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.db.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.db.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/13 17:47
 * @Version 1.0
 */

@DubboService
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    //保存用户
    @Override
    public Long save(User user) {

        //md5加密
        String md5 = SecureUtil.md5(user.getPassword());

        user.setPassword(md5);
        //保存用户
        userMapper.insert(user);

        //返回主键，前端需要用到
        return user.getId();

    }


    //通过电话号码查询用户
    @Override
    public User findByPhone(String phone) {

        //构建条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getMobile, phone);

        //查询
        return userMapper.selectOne(queryWrapper);

    }

    @Override
    public void update(User user) {

        userMapper.updateById(user);

    }


}
